--[[
RTS engine
author: Martin Dorazil
date: 14.3.2013
 
main gui script
 script for build game gui (resource bar, menu, etc.)
 for more help readme.txt

structure:
  _extern
    .screen - data type Form
    .resource - game resources
    .timing - game main fps counter

binded c++ data types:
  Form
  Label
  Button
  Resource
  Timer
]]--

-- global screen resolution
screenWidth = 0
screenHeight = 0
UPDATE_INTERVAL = 1000

-- setup game gui interface
function buildTopBar()
  topBar = Form(screenWidth, 50)
  topBar:setPos(0,0)
  topBar:setBackground("topbar.png")
  
  woodIcon = Form(50,50)
	woodIcon:setBackground("wood.png")
	woodIcon:setPos(30,0)

	goldIcon = Form(50,50);
	goldIcon:setBackground("money.png")
	goldIcon:setPos(150,0);

  wLabel = Label(200,16)
	wLabel:setPos(80, 17)
  wLabel:setCaption("0000")

  gLabel = Label(200,16)
	gLabel:setPos(210, 17)
  gLabel:setCaption("0000")

	fpsLabel = Label(200,16)
	fpsLabel:setPos(screenWidth-120, 17)
  fpsLabel:setCaption("FPS: 00")

  topBar:addItem(wLabel)
  topBar:addItem(gLabel)
  topBar:addItem(fpsLabel)
  topBar:addItem(woodIcon)
  topBar:addItem(goldIcon)

  -- add topBar on screen
  _extern.screen:addItem(topBar)
end

-- called on initialization engine
function _init()
  -- get screen size
	screenWidth = _extern.screen:getWidth()
  screenHeight = _extern.screen:getHeight()

  -- build top bar
  buildTopBar()

  updateTimer = Timer()
end

-- call every frame to update values
function _update()
  -- update resource info in time interval
  if updateTimer:GetInterval(UPDATE_INTERVAL) then
    -- get resource data from extern
    local wood = _extern.resource:getWood()
    local gold = _extern.resource:getGold()
  
    -- set resource labels
    wLabel:setCaption(tostring(wood))
    gLabel:setCaption(tostring(gold))
  end

  -- update fps every frame
  local fps = _extern.timing:update()
  fpsLabel:setCaption("FPS: " .. tostring(fps))
end

function _clean()
  updateTimer = nil
  topBar = nil
end